﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ft_module_parser
{
    class mdataUtilities
    {
        public static string mdataname = "M999";
        public enum dbType
        {
            pv_db,
            pv_field,
            stage_data,
            stage_data_base,
            obj_db,
            obj_db_base,
            tex_db,
            tex_db_base,
            auth_3d_db,
            mot_db,
            spr_db,
            spr_db_base,
            aet_db
        }
        internal static List<int> getMdatas(string mdatapath)
        {
            string path = "";

            var directories = Directory.GetDirectories(mdatapath);
            var mdatas = new List<int>();

            var outp = new StreamWriter(mdatapath + "\\M999\\info.txt", false, Encoding.ASCII);
            outp.NewLine = "\n";
            outp.WriteLine("#mdata_info");
            int counter = 0;
            foreach (var i in directories)
            {
                try
                {
                    var le_path = Path.GetFileName(i).Remove(0, 1);
                    mdatas.Add(int.Parse(le_path));
                    var id = int.Parse(le_path);
                    if (id < 998)
                    {
                        outp.WriteLine("depend." + counter + "=M" + id);
                        counter++;
                    }
                } catch
                {
                    //skip non-numeric ones rather than crashing
                }
            }
            outp.WriteLine("depend.length=" + (counter));
            outp.WriteLine("version=20161030");
            outp.Close();
            return mdatas;
        }

        internal static string getMdataPath(string mdatapath, string uric)
        {
            string path = "";
            string uri = "\\rom\\" + uric;

            var directories = Directory.GetDirectories(mdatapath);
            var mdatas = new List<int>();

            foreach (var i in directories)
            {
                try
                {
                    var le_path = Path.GetFileName(i).Remove(0, 1);
                    var id = int.Parse(le_path);
                    if (id < 998)
                    {
                        if (File.Exists(i + uri))
                            path = i + uri;
                    }
                } catch
                {
                    //skip non-numeric ones rather than crashing
                }
            }

            if (path == "")
            {
                throw new Exception("mdata fail " + directories + ";" + uri);
            }
            return path;
        }
        public static string getPath(string mdatapath, dbType type)
        {
            string path = mdatapath + "//" + mdataname;
            
            switch (type)
            {
                case dbType.pv_db:
                    path = getMdataPath(mdatapath, "mdata_pv_db.txt");
                    break;
                case dbType.pv_field:
                    path = getMdataPath(mdatapath, "mdata_pv_field.txt");
                    break;
                case dbType.obj_db:
                    path = getMdataPath(mdatapath, "objset\\mdata_obj_db.bin");
                    break;
                case dbType.tex_db:
                    path = getMdataPath(mdatapath, "objset\\mdata_tex_db.bin");
                    break;
                case dbType.obj_db_base:
                    try
                    {
                        path = getMdataPath(mdatapath, "objset\\obj_db.bin");
                    } catch
                    {
                        path = mdatapath + "\\..\\rom\\objset\\obj_db.bin";
                    }
                    break;
                case dbType.tex_db_base:
                    try
                    {
                        path = getMdataPath(mdatapath, "objset\\tex_db.bin");
                    }
                    catch
                    {
                        path = mdatapath + "\\..\\rom\\objset\\tex_db.bin";
                    }
                    break;
                case dbType.stage_data:
                    path = getMdataPath(mdatapath, "mdata_stage_data.bin");
                    break;
                case dbType.stage_data_base:
                    try
                    {
                        path = getMdataPath(mdatapath, "mdata_stage_data.bin");
                    }
                    catch
                    {
                        path = mdatapath + "\\..\\rom\\stage_data.bin";
                    }
                    break;
                case dbType.auth_3d_db:
                    path = getMdataPath(mdatapath, "auth_3d\\mdata_auth_3d_db.bin");
                    break;
                case dbType.mot_db:
                    path = getMdataPath(mdatapath, "rob\\mdata_mot_db.farc");
                    break;
                case dbType.spr_db:
                    path = getMdataPath(mdatapath, "2d\\mdata_spr_db.bin");
                    break;
                case dbType.spr_db_base:
                    path = mdatapath + "\\..\\2d\\spr_db.bin";
                    break;
                case dbType.aet_db:
                    path = getMdataPath(mdatapath, "2d\\mdata_aet_db.bin");
                    break;
            }
            return path;
        }
    }
}
